﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using ImageComboBox;

namespace PayloadTracker
{
    public delegate void OnNewFlightDelegate(Flight flight);
    public partial class NewFlight : Form
    {
        public event OnNewFlightDelegate OnNewFlight;

        APRS.APRSConfiguration m_Config;
        APRS.IAPRSProvider m_Provider;
        NewProvider m_NewProviderDialog;
        object[] m_ImageIndex = new object[]{"/",0};
        APRSSymbols m_APRSSymbols = new APRSSymbols();
        
        public NewFlight(APRS.APRSConfiguration config)
        {
            m_Provider = null;
            m_Config = config;
            InitializeComponent();
        }

        void m_NewProviderDialog_OnNewProvider(PayloadTracker.APRS.IAPRSProvider provider)
        {
            m_Config.APRSProviders.Add(provider);
            SetupProviders();
            cbProvider.SelectedItem = provider.Name;
        }
        private void NewFlight_Load(object sender, EventArgs e)
        {
            SetupColors();
            SetupProviders();
            SetupSymbols();
            SetupOverlays();
        }
       
        void SetupColors()
        {
            List<string> colors = new List<string>();
            foreach(string s in Enum.GetNames(typeof(KnownColor)))
            {
                KnownColor k = (KnownColor)Enum.Parse(typeof(KnownColor),s);
                if(k >= KnownColor.Transparent)
                    colors.Add(s);
            }


            //cbObject.Items.AddRange(colors.ToArray());
            cbPlot.Items.AddRange(colors.ToArray());


            //cbObject.SelectedItem = "Red";
            cbPlot.SelectedItem = "Black";
        }

        void SetupProviders()
        {
            cbProvider.Items.Clear();

            cbProvider.Items.Add("New...");
            foreach(APRS.IAPRSProvider p in m_Config.APRSProviders)
                cbProvider.Items.Add(p.Name);
        }

        void SetupSymbols()
        {
            comboBoxEx1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBoxEx1.ImageList = m_APRSSymbols.ImageList;
            for (int i = 0; i < m_APRSSymbols.SymbolMetaData.Length; i++)
            {
                comboBoxEx1.Items.Add(new ComboBoxExItem(m_APRSSymbols.SymbolMetaData[i][2], i));
            }
        }
        void SetupOverlays()
        {
            string[] strs = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value");
            foreach (string str in strs)
            {
                string[] tuple = { str, str };
                dt.Rows.Add(tuple);
            }
            this.comboBox1.DataSource = dt;
            this.comboBox1.ValueMember = "value";
            this.comboBox1.DisplayMember = "name";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (m_Provider == null)
            {
                MessageBox.Show("Please select an APRS Provider.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (txtCallsign.Text.Trim().Length == 0 && !chkAllStations.Checked)
            {
                MessageBox.Show("Please enter a callsign to filter.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            OnNewFlightDelegate handler = OnNewFlight;
            if (handler != null)
            {
                if (!m_Provider.TrackAll && chkAllStations.Checked)
                    m_Provider.TrackAll = true;

                handler(new Flight(m_Provider, txtCallsign.Text.Trim().ToUpper(), m_ImageIndex, Color.FromName(cbPlot.SelectedItem as string)));
            }

            this.Close();
        }

        private void cbProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbProvider.SelectedIndex == 0)
            {
                m_NewProviderDialog = new NewProvider();
                m_NewProviderDialog.OnNewProvider += new OnNewProviderDelegate(m_NewProviderDialog_OnNewProvider);
                m_NewProviderDialog.Show(this);
                return;
            }

            m_Provider = m_Config.APRSProviders[cbProvider.SelectedIndex - 1];

        }

        private void comboBoxEx1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int index = comboBoxEx1.SelectedIndex;
            int[] overlays = new int[] { 98, 101, 111, 125, 128, 150, 157, 158, 162, 173, 178, 181, 185 };
            m_ImageIndex[1] = index;
            if (index <= 95)
            {
                m_ImageIndex[0] = "/";
                comboBox1.Enabled = false;
            }
            else if (Array.IndexOf(overlays, comboBoxEx1.SelectedIndex) > -1)
            {
                m_ImageIndex[0] = "\\";
                comboBox1.Enabled = true;
                comboBox1.SelectedValue = "0";
            }
            else
            {
                m_ImageIndex[0] = "\\";
                comboBox1.Enabled = false;
            }
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            m_ImageIndex[0] = comboBox1.SelectedValue;
        }

        private void chkAllStations_CheckedChanged(object sender, EventArgs e)
        {
            txtCallsign.Enabled = !chkAllStations.Checked;
            comboBoxEx1.Enabled = !chkAllStations.Checked;
            comboBox1.Enabled = !chkAllStations.Checked;
            cbPlot.Enabled = !chkAllStations.Checked;
        }




        
    }
}
